/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "Vector3f.h"


/** Zero out the vector */
void Vector3f::ZeroOut()
{
    for( int i = 0; i < 3;i++ )
        m_data[i] = 0;
}
/** Length/magnatude of the vector */
float Vector3f::Length() const
{
    return sqrt(m_data[0]*m_data[0] + 
                m_data[1]*m_data[1] + 
                m_data[2]*m_data[2] );
}

/** Normalize the vector */
void Vector3f::Normalize()
{
    float length = Length();
    m_data[0] = m_data[0] / length;
    m_data[1] = m_data[1] / length;
    m_data[2] = m_data[2] / length;
}
/** Multiply two vectors */
void Vector3f::Multiply( const Vector3f &v )
{
    
}

/** Multiply by a scalar */
Vector3f Vector3f::Multiply( float scalar ) const
{
    return Vector3f(    m_data[0] * scalar,
                        m_data[1] * scalar,
                        m_data[2] * scalar );
}
/** Divide by a scalar */
Vector3f Vector3f::Divide( float scalar ) const
{   // do not divide by 0
    scalar = (scalar) ? scalar : 1;
    return Vector3f(    m_data[0] / scalar,
                        m_data[1] / scalar,
                        m_data[2] / scalar );
}
/** Add */
Vector3f Vector3f::Add( Vector3f &v ) const
{
    return Vector3f(    m_data[0] + v[0], 
                        m_data[1] + v[1],
                        m_data[2] + v[2] );
}

/** Subtract */
Vector3f Vector3f::Subtract( Vector3f &v ) const
{
    return Vector3f(    m_data[0] - v[0], 
                        m_data[1] - v[1],
                        m_data[2] - v[2] );
}

/** Dot Product of two vectors */
float Vector3f::DotProduct( Vector3f &v )
{
    return  m_data[0]*v[0] +
            m_data[1]*v[1] +
            m_data[2]*v[2];
}

/** Cross Product of two vectors */
Vector3f Vector3f::CrossProduct( Vector3f &v ) const
{
    return Vector3f(    m_data[1] * v[2] - m_data[2] * v[1],
                        m_data[2] * v[0] - m_data[0] * v[2],
                        m_data[0] * v[1] - m_data[1] * v[0] );
}

/** Angle between two vectors */
float Vector3f::Angle( Vector3f &v )
{
    // get the dotproduct of the two
    float dot = DotProduct( v );
    float len = Length();
    float len2 = v.Length();
    // now take the arc cos 
    return acos( dot/ ( len * len2 ) );
}

/** Set the vector */
void Vector3f::Set( float x, float y, float z )
{
    m_data[0] = x; m_data[1] = y; m_data[2] = z;
}
void Vector3f::Set( float* v )
{
   Set( v[0], v[1], v[2] ); 
}

void Vector3f::Set( const Vector3f &v )
{
    Set( v.GetX(), v.GetY(), v.GetZ() ); 
}